/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nxlv2.nomad.view.ficheClient;

import com.nxlv2.nomad.accessdb.services.ClientServices;
import com.nxlv2.nomad.accessdb.services.ContactServices;
import com.nxlv2.nomad.businessobjects.Client;
import com.nxlv2.nomad.businessobjects.Contact;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/**
 *
 * @author Administrateur
 */
public final class PanelContact extends JPanel {
    
    private final Client client;
    
    public PanelContact(Client client) {
        super();
        this.client = client;
        
        setLayout(new FlowLayout());
        
        JButton addContact = new JButton(new AbstractAction("Ajouter contact") {            
            @Override
            public void actionPerformed(ActionEvent e) {
                Contact ct = new Contact();
                ct.setNom(getNomCtTField().getText());
                ct.setPrenom(getPrenomCtTField().getText());
                ct.setMail(getMailCtTField().getText());
                ct.setTel(getTelCtTField().getText());

                ContactServices srvCt = new ContactServices();
                srvCt.create(ct,getClient());
                setListContact();
            }
        });
          
        add(addContact);
        add(buildContactPane());
        add(new JScrollPane(listContact));
        setListContact();
    }

    private final JList listContact = new JList();
    private JTextField nomCtTField;
    private JTextField prenomCtTField;
    private JTextField mailCtTField;
    private JTextField telCtTField;

    private JPanel buildContactPane() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        JLabel contactLabel = new JLabel("Contact");
        gbc.gridx = gbc.gridy = 0;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridheight = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.insets = new Insets(10, 15, 0, 0);
        panel.add(contactLabel, gbc);

        /* nom contact */
        JLabel nomCtLabel = new JLabel("Nom");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.;
        gbc.weighty = 0.;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(10, 15, 0, 0);
        panel.add(nomCtLabel, gbc);

        nomCtTField = new JTextField();
        nomCtTField.setColumns(10);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.BASELINE;
        gbc.insets = new Insets(0, 15, 0, 10);
        panel.add(nomCtTField, gbc);

        /* prenom contact */
        JLabel prenomCtLabel = new JLabel("Prenom");
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.;
        gbc.weighty = 0.;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(10, 15, 0, 0);
        panel.add(prenomCtLabel, gbc);

        prenomCtTField = new JTextField();
        prenomCtTField.setColumns(10);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.BASELINE;
        gbc.insets = new Insets(0, 15, 0, 10);
        panel.add(prenomCtTField, gbc);

        /* mail contact */
        JLabel mailCtLabel = new JLabel("Mail");
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.;
        gbc.weighty = 0.;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(10, 15, 0, 0);
        panel.add(mailCtLabel, gbc);

        mailCtTField = new JTextField();
        mailCtTField.setColumns(10);
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.BASELINE;
        gbc.insets = new Insets(0, 15, 0, 10);
        panel.add(mailCtTField, gbc);

        /* tel contact */
        JLabel telCtLabel = new JLabel("Téléphone");
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.;
        gbc.weighty = 0.;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.BASELINE_LEADING;
        gbc.insets = new Insets(10, 15, 0, 0);
        panel.add(telCtLabel, gbc);

        telCtTField = new JTextField();
        telCtTField.setColumns(10);
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.BASELINE;
        gbc.insets = new Insets(0, 15, 0, 10);
        panel.add(telCtTField, gbc);

        return panel;
    }

    public void setListContact() {
        ClientServices clSrv = new ClientServices();
        List<Contact> contacts = clSrv.getContacts(client);
        this.client.setListContact(contacts);

        int nbContact = contacts.size();
        String[] names = new String[nbContact];
        for (int i = 0; i < nbContact; i++) {
            names[i] = contacts.get(i).getNom();
        }

        listContact.setListData(names);
    }

    public JTextField getNomCtTField() {
        return nomCtTField;
    }

    public JTextField getPrenomCtTField() {
        return prenomCtTField;
    }

    public JTextField getMailCtTField() {
        return mailCtTField;
    }

    public JTextField getTelCtTField() {
        return telCtTField;
    }

    public Client getClient() {
        return client;
    }
    
}
